package com.woniu.consumer;

import com.rabbitmq.client.Channel;
import com.woniu.entity.Order;
import com.woniu.entity.RabbitmqConst;
import com.woniu.mapper.OrderMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 获取订单状态
 * 判断是否是未付款状态
 *      是：改成取消
 *      不是：什么都不做
 */


@Component
public class CheckOrderStatConsumer {
    @Resource
    private OrderMapper orderMapper;
    @RabbitListener(queues = RabbitmqConst.CHECKORDERSTAT_QUEUE)
    public void receive(String msg, Channel channel , Message message){
        try {
            //1.查询订单状态
            Order order = orderMapper.queryOrderStatByOrderNum(msg);
            boolean equals = order.getStatus().equals(1);
            if (equals){
                System.out.println(msg);
                Order order1 = new Order();
                order1.setOrderNum(msg);
                order1.setStatus(4);
                orderMapper.updateOrderStatus(order1);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }
    }
}
